package com.neusoft.shopping.manager.dao;
import com.neusoft.shopping.manager.entity.BusinesEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BusinesDAOImpl {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    RowMapper<BusinesEntity> rowMapper = new BeanPropertyRowMapper<>(BusinesEntity.class);

    public BusinesEntity login(String account, String password) {
        String sql = "select * from businesinfo where account=? and password=?";
        try {
            return jdbcTemplate.queryForObject(sql, rowMapper, account, password);
        }catch (EmptyResultDataAccessException e){
            return null;
        }

    }
    public List<BusinesEntity> qureyAll() {
        String sql = "select * from businesinfo";
        return jdbcTemplate.query(sql, rowMapper);
    }
    public int add_busines(BusinesEntity busines){
        String sql="insert into businesinfo values (null,?,?,?,?)";
        return jdbcTemplate.update(sql,busines.getBusinesname(),busines.getPhone(),busines.getAccount(),busines.getPassword());
    }
    public int update_buines(BusinesEntity busines){
        String sql="update busines set businesname=?,phone=?,account=?,password=? where businesno=?";
        return jdbcTemplate.update(sql,busines.getBusinesname(),busines.getPhone(),busines.getAccount(),busines.getPassword(),busines.getBusinesno());
    }
    public int del_buines(Integer businesno){
        String sql="delete from businesinfo where businesno=?";
        return jdbcTemplate.update(sql,businesno);
    }
}

